﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Pavilion;

namespace Pavilion.GameTools
{
    public class MouseCursorTool : GameToolBase
    {
        private MouseCursor mouseCursor;
        public event Action<MouseCursor> MouseMoved, LeftButtonPressed, LeftButtonReleased, RightButtonPressed, RightButtonReleased, ScrollWheelChanged;

        public MouseCursorTool(GameItem parentGameItem) : base(parentGameItem)
        {
            CreateMouseCursor(PlayerMouse.Name);
        }

        public void Dispose()
        {
            DisposeCurrentMouseCursor();
        }

        public override void Load(XElement xmlProperties)
        {
            MouseCursorType = xmlProperties.Attribute("MouseCursorType").Value;

            ParentGameItem.Disposed += (x) => { DisposeCurrentMouseCursor(); };

            ParentGameItem.UpdateLogic += (GameTime gameTime) => { mouseCursor.Update(gameTime); };
        }

        private void CreateMouseCursor(string keyboardType)
        {
            if (keyboardType == PlayerMouse.Name)
                mouseCursor = new PlayerMouse();
            else if (keyboardType == ArtificialMouse.Name)
                mouseCursor = new ArtificialMouse();
            else
                return;

            mouseCursor.Initialize(1f, 250, 250, 280, 190);
            mouseCursor.LeftMousePressed += new Action<MouseCursor>(MouseCursor_LeftMousePressed);
            mouseCursor.LeftMouseReleased += new Action<MouseCursor>(MouseCursor_LeftMouseReleased);
            mouseCursor.RightButtonPressed += new Action<MouseCursor>(MouseCursor_RightButtonPressed);
            mouseCursor.RightButtonReleased += new Action<MouseCursor>(MouseCursor_RightButtonReleased);
            mouseCursor.ScrollWheelChanged += new Action<MouseCursor>(MouseCursor_ScrollWheelChanged);
            mouseCursor.MouseMoved += new Action<MouseCursor>(MouseCursor_MouseMoved);
        }

        private void DisposeCurrentMouseCursor()
        {
            if (mouseCursor == null)
                return;

            mouseCursor.LeftMousePressed -= new Action<MouseCursor>(MouseCursor_LeftMousePressed);
            mouseCursor.LeftMouseReleased -= new Action<MouseCursor>(MouseCursor_LeftMouseReleased);
            mouseCursor.RightButtonPressed -= new Action<MouseCursor>(MouseCursor_RightButtonPressed);
            mouseCursor.RightButtonReleased -= new Action<MouseCursor>(MouseCursor_RightButtonReleased);
            mouseCursor.ScrollWheelChanged -= new Action<MouseCursor>(MouseCursor_ScrollWheelChanged);
            mouseCursor.MouseMoved -= new Action<MouseCursor>(MouseCursor_MouseMoved);

            mouseCursor = null;
        }

        public string MouseCursorType
        {
            get { return mouseCursor.GetType().Name; }
            set
            {
                if (mouseCursor != null && mouseCursor.GetType().Name == value)
                    return;

                DisposeCurrentMouseCursor();
                CreateMouseCursor(value);
            }
        }

        /// <summary>
        /// Gets the position of the mouse cursor.
        /// </summary>
        public Vector2 Position
        {
            get { return mouseCursor.Position; }
        }

        protected void OnScrollWheelChanged()
        {
            if (ScrollWheelChanged != null)
                ScrollWheelChanged(mouseCursor);
        }

        protected void OnLeftButtonPressed()
        {
            if (LeftButtonPressed != null)
                LeftButtonPressed(mouseCursor);
        }

        protected void OnLeftButtonReleased()
        {
            if (LeftButtonReleased != null)
                LeftButtonReleased(mouseCursor);
        }

        protected void OnRightButtonPressed()
        {
            if (RightButtonPressed != null)
                RightButtonPressed(mouseCursor);
        }

        protected void OnRightButtonReleased()
        {
            if (RightButtonReleased != null)
                RightButtonReleased(mouseCursor);
        }

        private void OnMouseMoved()
        {
            if (MouseMoved != null)
                MouseMoved(mouseCursor);
        }

        private void MouseCursor_ScrollWheelChanged(MouseCursor mouseCursor)
        {
            OnScrollWheelChanged();
        }

        private void MouseCursor_RightButtonReleased(MouseCursor mouseCursor)
        {
            OnRightButtonReleased();
        }

        private void MouseCursor_RightButtonPressed(MouseCursor mouseCursor)
        {
            OnRightButtonPressed();
        }

        private void MouseCursor_LeftMouseReleased(MouseCursor mouseCursor)
        {
            OnLeftButtonReleased();
        }

        private void MouseCursor_LeftMousePressed(MouseCursor mouseCursor)
        {
            OnLeftButtonPressed();
        }

        private void MouseCursor_MouseMoved(MouseCursor mouseCursor)
        {
            OnMouseMoved();
        }
    }
}
